package com.jdy.singleton.register;

/**
 * 伪线程安全
 * 属于注册式单例模式
 * 即装载式单例
 * <p>
 * 案例： ThreadLocal来实现多数据源动态切换
 */
public class ThreadLocalSingleton {

    /**
     * ThreadLocal实现单例模式
     */
    private static final ThreadLocal<ThreadLocalSingleton> local = new ThreadLocal<ThreadLocalSingleton>() {
        @Override
        protected ThreadLocalSingleton initialValue() {
            return new ThreadLocalSingleton();
        }
    };

    /**
     * 私有化构造方法
     */
    private ThreadLocalSingleton() {
    }


    /**
     * 提供全局访问点
     *
     * @return 单例对象
     */
    public static ThreadLocalSingleton getInstance() {
        return local.get();
    }
}
